package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.workflow.model.bpm.BpmNodePrivilege;
import com.ccp.dev.workflow.model.bpm.BpmNodeSign;
import com.ccp.dev.workflow.service.bpm.BpmNodeSignService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ClassDescribe:
 * 会签任务投票规则 控制类
 * @author :wangcheng
 * Date: 2019-09-12
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/bpmNodeSign")
@Action(ownermodel = SysAuditModelType.PROCESS_MANAGEMENT)
public class BpmNodeSignController extends SysBaseController {
    @Resource
    private BpmNodeSignService bpmNodeSignService;

    /**
     * 跳转编辑会签任务投票规则
     * @param request http请求对象
     * @return 跳转地址
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/edit")
    @Action(description = "跳转编辑会签任务投票规则页面", detail = "跳转编辑会签任务投票规则页面")
    public String viewEdit(HttpServletRequest request, Model model) {
        String procDefId = RequestUtil.getString(request, "procDefId");
        String nodeId = RequestUtil.getString(request, "nodeId");

        BpmNodeSign bpmNodeSign = bpmNodeSignService.getByProcDefIdAndNodeId(procDefId, nodeId);
        if (BeanUtils.isEmpty(bpmNodeSign)) {
            bpmNodeSign = new BpmNodeSign();
            bpmNodeSign.setProcDefId(procDefId);
            bpmNodeSign.setNodeId(nodeId);
        }

        // 0=拥有所有特权，1=允许直接处理，2=允许一票制，3=允许补签
        // 这里顺序不能乱
        List<String> modeList = Arrays.asList("所有特权,允许直接处理,允许一票制,允许补签".split(StringPool.COMMA));
        List<BpmNodePrivilege> privileges = bpmNodeSignService.queryPrivilegesByProcDefIdAndNodeId(procDefId, nodeId);
        BpmNodePrivilege[] bpmNodePrivilegeArray = new BpmNodePrivilege[modeList.size()];
        if (BeanUtils.isNotEmpty(privileges)) {
            for (BpmNodePrivilege privilege : privileges) {
                if (privilege.getPrivilegeMode() == null) {
                    continue;
                }
                bpmNodePrivilegeArray[privilege.getPrivilegeMode().intValue()] = privilege;
            }
        }
        model.addAttribute("bpmNodeSign", bpmNodeSign)
                .addAttribute("modeList", modeList)
                .addAttribute("bpmNodePrivilegeArray", bpmNodePrivilegeArray);

        return getAutoView();
    }

    /**
     * 添加或更新会签任务投票规则
     * @param request http请求
     * @param bpmNodeSign 前台请求数据封装会签对象
     * @return 操作结果封装
     */
    @RequestMapping("/save")
    @ResponseBody
    @Action(description = "添加或更新会签任务投票规则", detail = "添加或更新会签任务投票规则")
    public ResultData save(HttpServletRequest request, BpmNodeSign bpmNodeSign) {

        // 处理特权列表
        List<BpmNodePrivilege> privileges = new ArrayList<>();
        String[] userTypes = request.getParameterValues("userType");
        String[] cmpIds = request.getParameterValues("cmpIds");
        String[] cmpNames = request.getParameterValues("cmpNames");
        for (int i = 0; i < userTypes.length; i++) {
            String userType = userTypes[i];
            String cmpId = cmpIds[i];
            String cmpName = cmpNames[i];
            if (StringUtil.isEmpty(userType) || StringUtil.isEmpty(cmpName)) {
                continue;
            }

            BpmNodePrivilege privilege = new BpmNodePrivilege();
            privilege.setPrivilegeMode((long) i);
            privilege.setUserType(Long.valueOf(userType));
            privilege.setCmpIds(cmpId);
            privilege.setCmpNames(cmpName);
            privileges.add(privilege);
        }

        try {
            bpmNodeSignService.addOrUpdateSignAndPrivilege(bpmNodeSign, privileges);
            return getResultDataSuccess("save.success");
        }catch (Exception e) {
            return getResultDataError("save.failure");
        }
    }
}
